package com.paradise.phone.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.paradise.api.FaceLogin;
import com.paradise.api.FacePhone;
import com.paradise.api.FacephoneListener;
import com.paradise.phone.R;
import com.paradise.phone.utils.CrashHandler;
import com.paradise.phone.utils.ToastUtil;

import org.linphone.core.LinphoneAuthInfo;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneProxyConfig;

/**
 * Created by Sisyphus on 2017/9/5.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener,
        FacephoneListener.FacephoneRegistrationStateListener{

    private EditText etUsername;
    private EditText etPassword;
    private EditText etDisplayName;
    private Button btnLogin;
    private ImageView ivLogo;
    private RelativeLayout rlLogin;

    private ImageView ivDisplayNameClear;
    private ImageView ivUsernameClear;
    private ImageView ivPasswordClear;

    private RelativeLayout rlProgress;

    private LinphoneAuthInfo authInfo;

    private CheckBox cbOpenCrashHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

        initEvent();
    }

    private void initView(){
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_pwd);
        etDisplayName = (EditText) findViewById(R.id.et_display_name);
        btnLogin = (Button) findViewById(R.id.btn_login);
        ivLogo = (ImageView) findViewById(R.id.iv_logo);
        rlLogin = (RelativeLayout) findViewById(R.id.rl_login);
        ivPasswordClear = (ImageView) findViewById(R.id.iv_password_clear);
        ivDisplayNameClear = (ImageView) findViewById(R.id.iv_diplay_name_clear);
        ivUsernameClear = (ImageView) findViewById(R.id.iv_username_clear);
        rlProgress = (RelativeLayout) findViewById(R.id.rl_progress);
        cbOpenCrashHandler = (CheckBox) findViewById(R.id.cb_open_crash_handler);

        if ((authInfo = FaceLogin.instance().getCurrentAuthInfo()) != null){
            etUsername.setText(authInfo.getUsername());
            etUsername.setText(authInfo.getPassword());
        }

        etDisplayName.setText(getSharedPreferences("face_phone", MODE_PRIVATE)
                .getString("display_name", ""));

        SharedPreferences pref = getSharedPreferences("face_phone", MODE_PRIVATE);
        cbOpenCrashHandler.setChecked(pref.getBoolean("open_crash_handler", true));
    }

    private void initEvent(){
        btnLogin.setOnClickListener(this);
        ivUsernameClear.setOnClickListener(this);
        ivDisplayNameClear.setOnClickListener(this);
        ivPasswordClear.setOnClickListener(this);

        cbOpenCrashHandler.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences pref = LoginActivity.this.getSharedPreferences("face_phone", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("open_crash_handler", isChecked);
                if (isChecked){
                    CrashHandler.getInstance().init(LoginActivity.this);
                }
            }
        });

        FacePhone.instance().addRegistrationStateListener(this);
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(rlLogin, "alpha", 0f, 1f);
        ObjectAnimator translateAnim = ObjectAnimator.ofFloat(rlLogin, "translationY", 0, -150f, 0);
        ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(rlLogin, "scaleY", 1f, 1.2f, 1f);
        AnimatorSet set = new AnimatorSet();
        set.play(alphaAnim).with(translateAnim).with(scaleYAnim);
        set.setDuration(1500);
        set.start();

        /*AnimationSet set = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_logo_set);
        rlLogin.setAnimation(set);
        set.setFillAfter(true);
        rlLogin.offsetTopAndBottom(-rlLogin.getHeight() / 2);
        rlLogin.startAnimation(set);*/
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn_login:
                if (TextUtils.isEmpty(etUsername.getText().toString())
                        || TextUtils.isEmpty(etDisplayName.getText().toString())
                        /*|| TextUtils.isEmpty(etPassword.getText().toString())*/){
                    Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    FaceLogin.instance().regist(this, etDisplayName.getText().toString(),
                            etUsername.getText().toString(),
                            getString(R.string.password),
                            getString(R.string.domain),
                            getString(R.string.proxy),
                            getString(R.string.transport));
                }

                break;

            case R.id.iv_diplay_name_clear:
                etDisplayName.setText("");
                break;

            case R.id.iv_username_clear:
                etUsername.setText("");
                break;

            case R.id.iv_password_clear:
                etPassword.setText("");
                break;
        }
    }


    @Override
    public void registrationState(LinphoneCore linphoneCore, LinphoneProxyConfig linphoneProxyConfig, LinphoneCore.RegistrationState registrationState, String s) {
        if (registrationState.equals(LinphoneCore.RegistrationState.RegistrationOk)){

            rlProgress.setVisibility(View.INVISIBLE);
            ToastUtil.showToast(this, "登陆成功");

            SharedPreferences sp = this.getSharedPreferences("face_phone", MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("display_name", etDisplayName.getText().toString());
            editor.commit();

            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra("LOGIN", "success");
            startActivity(intent);

            finish();

        }else if (registrationState.equals(LinphoneCore.RegistrationState.RegistrationProgress)){
            rlProgress.setVisibility(View.VISIBLE);
        }else if (registrationState.equals(LinphoneCore.RegistrationState.RegistrationFailed)){
            rlProgress.setVisibility(View.INVISIBLE);
            ToastUtil.showToast(this, "登陆失败");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
